package logModel

import (
	"git.oschina.net/zd04/beego-shop/utils"
	"github.com/astaxie/beego/orm"
	"time"
)

type Logs struct {
	Id         int64     `orm:"pk;column(id);"`
	Level      int8      `orm:"column(level);"`
	Msg        string    `orm:"column(msg);"`
	Module     string    `orm:"column(module);"`
	Class      string    `orm:"column(class);"`
	Func       string    `orm:"column(func);"`
	CreateTime time.Time `orm:"auto_now_add;type(datetime)"`
}

const (
	INFO = iota + 1
	WARNING
	ERROR
)

func (this *Logs) TableName() string {
	return utils.TableName("logs")
}

func init() {
	orm.RegisterModel(new(Logs))
}

func AddLog(module, class, function, msg string, level int8) (err error) {
	o := orm.NewOrm()
	o.Using("default")
	log := Logs{}

	log.Level = level
	log.Msg = msg
	log.Module = module
	log.Class = class
	log.Func = function
	// log.CreateTime = utils.NowYhmHis()
	_, err = o.Insert(&log)

	return err
}

func AddLogInfo(module, class, function, msg string) (err error) {
	return AddLog(module, class, function, msg, INFO)
}

func AddLogWarning(module, class, function, msg string) (err error) {
	return AddLog(module, class, function, msg, WARNING)
}

func AddLogError(module, class, function, msg string) (err error) {
	return AddLog(module, class, function, msg, ERROR)
}
